package cn.yomigaeri.warn.controller;

import cn.yomigaeri.common.entity.po.Rule;
import cn.yomigaeri.warn.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * @ClassName: RuleController
 * @Package: cn.yomigaeri.warn.controller
 * @Description:
 * @Author Yomigaeri
 * @Create 2025/6/5 12:05
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/warn/rules")
public class RuleController {

    private final RuleService ruleService;

    @Autowired
    public RuleController(RuleService ruleService) {
        this.ruleService = ruleService;
    }

    /**
     * 获取所有rules（带缓存）
     */
    @GetMapping
    public List<Rule> getAllRules() {
        return ruleService.getAllRulesWithCache();
    }

    /**
     * 手动刷新缓存
     */
    @PostMapping("/refresh-cache")
    public String refreshCache() {
        ruleService.refreshRuleCache();
        return "Rule cache refreshed successfully";
    }

    /**
     * 获取缓存状态
     */
    @GetMapping("/cache-status")
    public String getCacheStatus() {
        List<Rule> cachedRules = ruleService.getCachedRules();
        if (cachedRules == null) {
            return "Cache is empty";
        } else {
            return "Cache contains " + cachedRules.size() + " rules";
        }
    }
}